package com.test;

import com.bean.Dao.UserMapper;
import com.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserTest4 {
    @Test
    public void test01() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        List<User> userList = mapper.findAllResultMap();
        for(User user :userList){
            System.out.println(user);
        }
    }

    @Test
    public void test02() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        List<User> kaka = mapper.findByIdAndUsername(1, "kaka");
        for (User user:kaka) {
            System.out.println(user);
        }
    }

    @Test
    public void test03() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        User user = new User("kaka",null,null,null);
        List<User> user1 = mapper.findIdByUsername(user);
        for (User user2:user1) {
            System.out.println(user2);
        }
    }

    @Test
    public void test04() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);

        List<User> kaka = mapper.findByIdAndUsername1(1, "kaka");
        for (User user2:kaka) {
            System.out.println(user2);
        }
    }
    @Test
    public void test05() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        User user = new User("asaki",new Date(),"女","广州");
        mapper.saveUser(user);

    }
    @Test
    public void test06() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        User user = new User("asaki",new Date(),"女","广州");
        System.out.println(user);
        mapper.saveUser1(user);
        System.out.println(user);
        ss.close();
    }

    @Test
    public void test07() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        User user = new User("asaki",null,null,null);
        user.setId(7);
        List<User> o = mapper.findByUsernameAndIdIf(user);
        for (User u:
                o) {
            System.out.println(u);
        }
        ss.close();
    }
    @Test
    public void test08() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        User user = new User("asaki",null,null,null);
        user.setAddress("US");
        user.setId(3);
        mapper.updateSet(user);
    }
    @Test
    public void test09() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(5);
        List<User> l = mapper.findByIds1(list);
        for (User u:
             l) {
            System.out.println(u);
        }
    }
    @Test
    public void test10() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        SqlSession ss = ssf.openSession(true);

        UserMapper mapper = ss.getMapper(UserMapper.class);
        Integer[] list = {1,2,3};
        List<User> l = mapper.findByIds2(list);
        for (User u:
                l) {
            System.out.println(u);
        }
    }
}
